package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class TUploadPeer extends BasePeer{
	public static final String tableName = "TUpload";
	public static final String USERID = "userId";
	public static final String UPLOADID = "uploadId";
	public static final String PROJECTID = "projectId";
	public static final String CATEGORYID = "categoryId";
	public static final String FOLDERID = "folderId";
	public static final String STATE = "state";
	public static final String UPLOADDATE = "uploadDate";
	public static final String LASTDOWNLOAD = "lastDownload";
	public static final String NAME = "name";
	public static final String SIZE = "size";
	public static final String PUBLICPOLICY = "publicPolicy";
	public static final String FILETYPE = "filetype";
	public static final String[] columnNames=new String[]{"userId","uploadId","projectId","categoryId","folderId","state","uploadDate","lastDownload","name","size","publicPolicy","filetype"};
	public static TUpload getTUploadByPKs(Object categoryId,Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TUploadPeer.tableName,TUploadPeer.columnNames," TRUE  AND `categoryId`=?  AND `uploadId`=? ",new Object[]{categoryId,uploadId,},0, 1,conn);
			TUpload tUpload = new TUpload();
		fill(rs,tUpload);
		return tUpload;
	}
	public static TUpload getTUploadReferenceColumnsOnlyByPKs(Object categoryId,Object uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TUploadPeer.tableName,new String[]{"categoryId","uploadId",}," TRUE AND `categoryId`=?AND `uploadId`=?",new Object[]{categoryId,uploadId,}, 0,  1,conn);
			TUpload tUpload = new TUpload();
		fill(rs,tUpload);
		return tUpload;
	}
	public static TUpload getTUploadByUploadId(long uploadId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TUploadPeer.tableName,TUploadPeer.columnNames,"`uploadId`=?",new Object[]{uploadId},0, 1,conn);
			TUpload tUpload = new TUpload();
		fill(rs,tUpload);
		return tUpload;
	}
	public static TUpload getTUploadByCategoryId(long categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(TUploadPeer.tableName,TUploadPeer.columnNames,"`categoryId`=?",new Object[]{categoryId},0, 1,conn);
			TUpload tUpload = new TUpload();
		fill(rs,tUpload);
		return tUpload;
	}
	public static void fill(ResultSet rs, TUpload tUpload) throws SQLException {
		if(rs.next()){
			try{tUpload.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{tUpload.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{tUpload.setProjectId(rs.getLong("projectId"));}catch(Exception ex){}
			try{tUpload.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			try{tUpload.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{tUpload.setState(rs.getInt("state"));}catch(Exception ex){}
			try{tUpload.setUploadDate(rs.getTimestamp("uploadDate"));}catch(Exception ex){}
			try{tUpload.setLastDownload(rs.getTimestamp("lastDownload"));}catch(Exception ex){}
			try{tUpload.setName(rs.getString("name"));}catch(Exception ex){}
			try{tUpload.setSize(rs.getLong("size"));}catch(Exception ex){}
			try{tUpload.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{tUpload.setFiletype(rs.getString("filetype"));}catch(Exception ex){}
			tUpload.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<TUpload> tUploadCollection) throws SQLException {
		while(rs.next()){
			TUpload tUpload=new TUpload();
			try{tUpload.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{tUpload.setUploadId(rs.getLong("uploadId"));}catch(Exception ex){}
			try{tUpload.setProjectId(rs.getLong("projectId"));}catch(Exception ex){}
			try{tUpload.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			try{tUpload.setFolderId(rs.getLong("folderId"));}catch(Exception ex){}
			try{tUpload.setState(rs.getInt("state"));}catch(Exception ex){}
			try{tUpload.setUploadDate(rs.getTimestamp("uploadDate"));}catch(Exception ex){}
			try{tUpload.setLastDownload(rs.getTimestamp("lastDownload"));}catch(Exception ex){}
			try{tUpload.setName(rs.getString("name"));}catch(Exception ex){}
			try{tUpload.setSize(rs.getLong("size"));}catch(Exception ex){}
			try{tUpload.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{tUpload.setFiletype(rs.getString("filetype"));}catch(Exception ex){}
			tUpload.resetModified();
			tUploadCollection.add(tUpload);
		}
	}
	public static boolean update(TUpload tUpload, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < TUploadPeer.columnNames.length; i++) {
			if (((TUploadBase)tUpload).modified[i]) {
				columns.add(TUploadPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `TUpload` SET ");
		sql.append("WHERE TRUE AND `categoryId` = ? AND `uploadId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,tUpload.get(column));
		}
		pstmt.setObject(columns.size()+1+0,tUpload.get("categoryId"));
		pstmt.setObject(columns.size()+1+1,tUpload.get("uploadId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(TUpload tUpload, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (tUpload.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `TUpload` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (tUpload.get(column) != null) {
				pstmt.setObject(i + 1 + j, tUpload.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<TUpload> getTUploadCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<TUpload> collection=new Vector<TUpload>();
		fill(rs,collection);
		return collection;
	}
}
